
package com.chuangan.releasetask.converter;

import com.chuangan.releasetask.enums.ResultEnum;
import com.chuangan.releasetask.exception.ReleaseTaskException;
import com.chuangan.releasetask.form.TaskForm;
import com.chuangan.releasetask.pojo.TaskMaster;
import com.chuangan.releasetask.pojo.TaskStep;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zihua
 * @Date: 2020/8/17 15:27
 * @Version 1.0
 */
@Slf4j
public class TaskForm2TaskMasterConverter {

    public static TaskMaster convert(TaskForm taskForm) {
        Gson gson = new Gson();
        TaskMaster taskMaster = new TaskMaster();
        taskMaster.setTaskName(taskForm.getTaskName());
        taskMaster.setTaskTitle(taskForm.getTaskTitle());
        taskMaster.setTaskHref(taskForm.getTaskHref());
        taskMaster.setTaskClass(taskForm.getTaskClass());
        taskMaster.setTaskPublisher(taskForm.getTaskPublisher());
        taskMaster.setTaskPhone(taskForm.getTaskPhone());
        taskMaster.setTaskFee(taskForm.getTaskFee());
        taskMaster.setTaskAmount(taskForm.getTaskAmount());
        taskMaster.setTaskNumber(taskForm.getTaskNumber());
        taskMaster.setStartTime(taskForm.getStartTime());
        taskMaster.setEndTime(taskForm.getEndTime());
        taskMaster.setCommitDeadline(taskForm.getCommitDeadline());
        taskMaster.setAuditDeadline(taskForm.getAuditDeadline());
        List<TaskStep> taskSteps = new ArrayList<>();
        try {
            taskSteps = gson.fromJson(taskForm.getItems(),
                    new TypeToken<List<TaskStep>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", taskForm.getItems());
            throw new ReleaseTaskException(ResultEnum.PARAM_ERROR);
        }
        taskMaster.setTaskStepList(taskSteps);

        return taskMaster;
    }






}






